<?php

namespace front\modules\app\controllers;

use Yii;
use front\models\SuggestionModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backfeed\services\SuggestionService;

/**
 * SuggestionController implements the CRUD actions for SuggestionModel model.
 */
class SuggestionController extends WebBaseController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all SuggestionModel models.
     * @return mixed
     * 用户建议和意见
     */
    public function actionIndex()
    {
        $data = yii::$app->request->get();
        $data['getByWeb']=1;
        $info = SuggestionService::search($data);
        WebBaseController::getAjaxData(1,'获取成功',$info['result']);
    }

    /**
     * Creates a new SuggestionModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     * 插入建议意见
     */
    public function actionCreate()
    {
       $data['user_id'] = intval(yii::$app->request->post('user_id'));
       $data['content'] = strip_tags(yii::$app->request->post('content'));
       if(!$data['user_id']){
           WebBaseController::getAjaxData(2,'请登录');
       }
       if(!$data['content']){
           WebBaseController::getAjaxData(3,'请填写内容');
       }
        $info = SuggestionService::add($data);
        WebBaseController::getAjaxData(1,'添加成功',$info['result']);
    }



    /**
     * Updates an existing SuggestionModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing SuggestionModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the SuggestionModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return SuggestionModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = SuggestionModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
